#
# Process this file with autoconf to produce a configure script.
#

AC_INIT([CCSO],[1.0],[404227815@qq.com, wangf44@gmail.com])
AC_CONFIG_SRCDIR(ccso/ccso.f90)
AC_CONFIG_HEADER(config.h)
AC_CONFIG_AUX_DIR(config.e)
AC_PREFIX_DEFAULT([/usr/local/ccso])
AC_PROG_MAKE_SET
AC_CANONICAL_HOST

AC_ARG_WITH([archlib], AC_HELP_STRING([--with-archlib=librarch],
	[use 'librarch' as default system library]))

AC_ARG_ENABLE([mpi], AC_HELP_STRING([--enable-mpi=vendor],
	[enable parallel version using mpich, lam, openmpi or scali]))

AC_ARG_ENABLE([gimic], AC_HELP_STRING([--enable-gimic],
	[enable the GIMIC interface]))

AC_ARG_ENABLE([pbsdsh], AC_HELP_STRING([--enable-pbsdsh],
	[use pbsdsh instead of 'mpiexec -comm none']))

AC_ARG_WITH([mpirun], AC_HELP_STRING([--with-mpirun=prog],
	[command to run programs in parallel]))

AC_ARG_WITH([exenodes], AC_HELP_STRING([--with-exenodes=prog],
	[command to run serial programs on all nodes]))

# Check for programs.

CHECK_GNU_MAKE
if test "$ifGNUmake" = "#"; then
	AC_MSG_ERROR([No working version of GNU make found!])
fi

AC_PROG_RANLIB
AC_PROG_INSTALL
AC_CHECK_PROGS(LD,ld,false)
AC_CHECK_PROGS(AR,ar,false)
AC_CHECK_PROGS(STRIP,strip,true)
AC_CHECK_PROGS(ETAGS,etags,true)
AC_CHECK_PROGS(CTAGS,ctags,true)
#AC_PROG_LN_S
#AC_PROG_AWK
#AC_PROG_YACC
#AC_PROG_LEX

# ACX_ macros are defined in aclocal.m4
# Setup compilers for different architechtures

case $host in 
	*i*86*-linux*)
	try_cc="gcc cc icc"
    try_cxx="g++ c++ icpc"
	try_fc="pgf90 ifort ifc f90 pgf77 g95 g77 gfort f77"
	build_type="linux_i386"
	archlibdir="librlinux"
	vprop="vprop_linux"
    cxxldflags="$cxxldflags -lrt"
	;;
	*i*86*-apple-darwin*)
	try_cc="gcc cc icc"
    try_cxx="g++ c++ icpc"
	try_fc="ifort g77 g95 pgf90 f90 pgf77 g95 gfort f77"
	build_type="darwin_i386"
	archlibdir="librlinux"
	vprop="vprop_linux"
	;;
	*ia64*-linux*)
	try_cc="gcc cc icc"
    try_cxx="g++ c++ icpc"
	try_fc="ifort pgf90 ifc f90 pgf77 g95 g77 gfortran f77"
	build_type="linux_ia64"
	archlibdir="librlinux"
	vprop="vprop_linux"
    cxxldflags="$cxxldflags -lrt"
	;;
	*x86_64*-linux*)
	try_cc="gcc cc icc"
    try_cxx="g++ c++ icpc"
	try_fc="pgf90 pgf77 ifort ifc f90 g95 g77 gfortran f77"
	build_type="x86_64-linux"
	archlibdir="librlinux"
	vprop="vprop_linux"
	cxxldflags="$cxxldflags -lrt"
	;;
	*alpha*-linux*)
	try_cc="ccc cc gcc"
    try_cxx="cxx g++ c++"
	try_fc="fort f90 f77 g77"
	build_type="linux_alpha"
	archlibdir="librlinux"
	vprop="vprop_linux"
	;;
	*dec*-osf*)
	try_cc="ccc cc gcc"
    try_cxx="cxx g++ c++"
	try_fc="fort f90 f77 g77"
	build_type="dec_osf"
	archlibdir="librdec"
	vprop="vprop"
	;;
	*ibm*-aix*)
	try_cc="cc gcc"
    try_cxx="g++ c++"
	try_fc="xlf xlf90 f90 f77 g77"
	build_type="ibm_aix"
	archlibdir="libribm"
	vprop="vprop"
	;;
	*sgi*-irix*)
	try_cc="cc gcc"
    try_cxx="g++ c++"
	try_fc="f77 g77 fort f90"
	build_type="sgi_irix"
	archlibdir="librsgi"
	vprop="vprop"
	;;
	*sun*-solaris*)
	try_cc="cc gcc"
    try_cxx="g++ c++"
	try_fc="f90 f77 g77"
	build_type="sun_solaris"
	archlibdir="librdec"
	vprop="vprop"
	;;
	hppa2.0w-hp-hpux11.*)
	try_cc="cc gcc"
    try_cxx="g++ c++"
	try_fc="f90 f77 g77"
	build_type="hppa2.0w-hp-hpux11"
	archlibdir="librhppa"
	vprop="vprop"
	;;
	ia64-hp-hpux11.2*)
	try_cc="cc"
	try_cxx="ac++"
	try_fc="f90"
	build_type="ia64-hp-hpux11.2x"
	archlibdir="librhpia64"
	vprop="vprop"
	;;
	*) 	
	AC_MSG_WARN([Unknown architecture! This might not work...])
	try_cc="cc gcc ccc cl"
	try_cxx="g++ c++"
	try_fc="f90 xlf90 pgf90 epcf90 f95 xlf95 lf95 fort \
	f77 xlf cf77 cft77 frt pgf77 fl32 af77 g77 fort77 fc"
	build_type="unknown"
	vprop="vprop"
	;;
esac

ax_save_CFLAGS="$CFLAGS"
ax_save_CXXFLAGS="$CXXFLAGS"
ax_save_FCFLAGS="$FCFLAGS"

AC_PROG_CC([$try_cc])
AC_PROG_CXX([$try_cxx])
AC_PROG_FC([$try_fc])

AC_LANG_PUSH([Fortran])
AC_OPENMP
AC_LANG_POP
AC_LANG_PUSH([C])
AC_OPENMP
AC_LANG_POP
AC_LANG_PUSH([C++])
AC_OPENMP
AC_LANG_POP

# Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS([string.h])

# Checks for library functions.
AC_FUNC_MALLOC
AC_CHECK_FUNCS([gethostname])

MPI_TYPE="none"
if test "x$enable_mpi" != "x"; then
	MPI_TYPE="$enable_mpi"
	AC_MSG_NOTICE([Checking if MPI is available])
	AC_LANG_PUSH([Fortran])
	ACX_MPI([],AC_MSG_ERROR([MPI not found: $enable_mpi]))
	AC_LANG_POP
	AC_LANG_PUSH([C])
	ACX_MPI([],AC_MSG_ERROR([MPI not found: $enable_mpi]))
	AC_LANG_POP
	AC_LANG_PUSH([C++])
	ACX_MPI([],AC_MSG_ERROR([MPI not found: $enable_mpi]))
	AC_LANG_POP
	case $enable_mpi in
		openmpi)
		AC_DEFINE([PAREXE(runcmd)], [ishell('mpirun ' // runcmd)],[openmpi]) 
        AC_DEFINE([EXENODES(runcmd)], [ishell('mpirun ' // runcmd)], [openmpi])
		if test "x$OPENMPI" = "x"; then
			tmp=`which orterun` 2>&1 >/dev/null
			case $tmp in
				/*openmpi*bin|/*OPENMPI*bin|/*OpenMPI*bin)
				OPENMPI=`echo $tmp|sed s/\/bin.*//`
			esac
		fi
		;;
		lam)
		AC_DEFINE([PAREXE(runcmd)], [ishell('mpirun C ' // runcmd)],
		[lam])
		AC_DEFINE([EXENODES(runcmd)], [ishell('eval lamexec N ' // runcmd)],
		[lam])
		;;
		mpich)
		AC_DEFINE([PAREXE(runcmd)], [ishell('mpiexec ' // runcmd)],
		[mpich])
		AC_DEFINE([EXENODES(runcmd)], 
		[ishell('mpiexec -comm none -pernode ' // runcmd)], [mpich])
		;;
		scali|scampi)
		AC_DEFINE([PAREXE(runcmd)], 
		[ishell('mpimon ' // runcmd // ' -- `cat jobnodes`')],
		[scali])
		AC_DEFINE([EXENODES(runcmd)], 
		[ishell('mpiexec -comm none -pernode ' // runcmd)], [scali])
		;;
		*) AC_MSG_ERROR([Unknown MPI implementation: $enable_mpi])
		;;
	esac
	AC_CACHE_SAVE
else
	AC_DEFINE([PAREXE(runcmd)], [ishell('eval ' // runcmd)], [serial])
    AC_DEFINE([EXENODES(runcmd)], [ishell('eval ' // runcmd)], [serial])
fi

if test "x$with_mpirun" != "x"; then
	AC_DEFINE_UNQUOTED([PAREXE(runcmd)],
		[ishell('$with_mpirun ' // runcmd)])
fi
if test "x$with_exenodes" != "x"; then
	AC_DEFINE_UNQUOTED([EXENODES(runcmd)], 
		[ishell('$with_exenodes ' // runcmd)])
fi

pbsdsh=no
if test "x$enable_pbsdsh" != "x"; then
	pbsdsh=yes
fi
AC_SUBST([pbsdsh],[$pbsdsh])
AC_SUBST(MPI_TYPE, [$MPI_TYPE])

# clear the default compiler flags. we don't want them!
FCFLAGS="$ax_save_FCFLAGS $OPENMP_FCFLAGS"
CFLAGS="$ax_save_CFLAGS"
CXXFLAGS="$ax_save_CXXFLAGS"
ACX_SUBST_BUILD_FLAGS([$build_type])

cxxflags="$cxxflags $OPENMP_CXXFLAGS"
cxxldflags="$cxxldflags $OPENMP_CXXFLAGS"

#save the real FC compiler
FORTRAN_COMPILER=$FC
C_COMPILER=$CC
CXX_COMPILER=$CXX
if test "x$enable_mpi" != "x"; then 
	FC=$MPIFC
	fcflags="$fcflags -DMPI"
    CC=$MPICC
    cflags="$cflags -DMPI"
    CXX=$MPICXX
    cxxflags="$cxxflags -DMPI"
fi

AC_FC_LIBRARY_LDFLAGS
AC_FC_MAIN
AC_CACHE_SAVE

AX_FC_SEARCH_PATH

# This is needed for xalloc and friends
AC_CHECK_SIZEOF(void *)
AC_CHECK_SIZEOF(int)
AC_CHECK_SIZEOF(long int)
AC_CHECK_SIZEOF(long long int)

if test "$ac_cv_sizeof_int" = "8" ; then
	AC_DEFINE(FC_INT8_TYPE, [int], [FC int type])
else if test "$ac_cv_sizeof_long_int" = "8" ; then
	AC_DEFINE(FC_INT8_TYPE, [long int], [FC int type])
else if test "$ac_cv_sizeof_long_long_int" = "8" ; then
	AC_DEFINE(FC_INT8_TYPE, [long long int], [FC int type])
else
	AC_MSG_WARN([Could not determine type of 64-bit integer...])
	AC_DEFINE(FC_INT8_TYPE, [long long int], [FC int type])
fi; fi; fi


dnl AC_F77_LIBRARY_LDFLAGS
#AC_FC_LIBRARY_LDFLAGS
AC_FC_WRAPPERS

#AC_LANG_PUSH([Fortran])
#AC_FC_SRCEXT([f90])
#AC_LANG_POP([Fortran])

AC_LANG_PUSH([Fortran])

ACX_LAPACK(,[BLAS_LIBS=-lblashc
  LAPACK_LIBS=-llapackhc
  AC_MSG_WARN([No system BLAS/LAPACK libraries found! 
   *** Using handcoded BLAS/LAPACK instead ***])
])

#ACX_BLAS(,[BLAS_LIBS=-lblashc
#  AC_MSG_WARN([No system BLAS library found! 
#   *** Using handcoded BLAS instead ***])
#])

#if test "x$with_lapack" != "x" -o "x$with_lapack_dir" != "x"; then 
#  ACX_LAPACK(,AC_MSG_WARN([No working lapack library found!]))
#fi
AC_LANG_POP([Fortran])

AC_SUBST(FC_MODEXT,[])
#AX_F90_MODULE_EXTENSION
#if test "x$ax_f90_modext" = "xunknown" ; then
#     AC_MSG_ERROR([unable to find f90 modules extension])
#fi

AC_CACHE_SAVE


AC_MSG_CHECKING([for bin directory])
acx_bdir_ok=no
test -d bin && acx_bdir_ok=yes
AC_MSG_RESULT([$acx_bdir_ok])
if test "x$acx_bdir_ok" != "xyes"; then
	AC_MSG_NOTICE([creating bin])
	mkdir bin
fi

AC_MSG_CHECKING([for lib directory])
acx_bdir_ok=no
test -d lib && acx_bdir_ok=yes
AC_MSG_RESULT([$acx_bdir_ok])
if test "x$acx_bdir_ok" != "xyes"; then
	AC_MSG_NOTICE([creating lib])
	mkdir lib
fi

AC_MSG_CHECKING([for share directory])
acx_sdir_ok=no
test -d share && acx_sdir_ok=yes
AC_MSG_RESULT([$acx_sdir_ok])
if test "x$acx_sdir_ok" != "xyes"; then
	AC_MSG_NOTICE([creating share])
	mkdir share
fi

AC_SUBST(FORTRAN_COMPILER,$FORTRAN_COMPILER)
#ACX_SUBST_BUILD_FLAGS
if test "x$with_archlib" != "x"; then
  AC_SUBST(archlibdir, $with_archlib)
else
  AC_SUBST(archlibdir, $archlibdir)
fi
AC_SUBST(vprop, $vprop)
AC_SUBST(top_bindir,['$(builddir)/bin'])
AC_SUBST(top_includedir,['$(topdir)/include'])
AC_SUBST(top_libdir,['$(builddir)/lib'])
AC_SUBST(top_datadir,['$(builddir)/share'])


# substitutions for qcfour
if test "x$enable_mpi" != "x"; then
	AC_SUBST(paratype, [$MPI_TYPE])
	AC_SUBST(jobtype, [parallel])
	AC_SUBST(parcfour, [$prefix])
	AC_SUBST(cfour, [unknown])
else
	AC_SUBST(paratype, [serial])
	AC_SUBST(jobtype, [serial])
	AC_SUBST(cfour, [$prefix])
	AC_SUBST(parcfour, [unknown])
fi

if test "x$enable_gimic" = "xyes"; then
	AC_MSG_NOTICE([Enabling the GIMIC interface])
	AC_DEFINE(ENABLE_GIMIC,[],[Defined if GIMIC is to be compiled])
	AC_SUBST(ENABLE_GIMIC,yes)
else
	AC_MSG_NOTICE([Not enabling the GIMIC interface])
	AC_SUBST(ENABLE_GIMIC,no)
fi

AC_CONFIG_FILES([Makefile make.rules make.config 
lib_mod/Makefile:lib_mod/Makefile
libsrint/Makefile:libsrint/Makefile
cc_pyscf/Makefile:cc_pyscf/Makefile
ccsd/Makefile:ccsd/Makefile
ccsdt/Makefile:ccsdt/Makefile
uccsd/Makefile:uccsd/Makefile
uccsdt/Makefile:uccsdt/Makefile
roccsdt/Makefile:roccsdt/Makefile
ccsd_ri/Makefile:ccsd_ri/Makefile
uccsd_ri/Makefile:uccsd_ri/Makefile])
AC_OUTPUT

